package ObjectVC;

import java.util.ArrayList;

public aspect VCOperations {
	public pointcut VCops(Object actualObj, Action action, ArrayList<Object> actionParams) :  
		execution(void ObjectActions.setAction(Object, Action, ArrayList<Object>)) 
		&& args(actualObj, action, actionParams);
	
/*
	after(Object actualObj, Action action, ArrayList<Object> actionParams) :  
		execution(void ObjectActions.setAction(Object, Action, ArrayList<Object>)) 
		&& args(actualObj, action, actionParams) && !within(ObjectVC){
		Object[] params = actionParams.toArray();
		
		Object obj = null;
		Date timestamp = null;
		String comment = null;
		Integer idx = null;
		VCMode vcMode = null;
		DeleteMode delMode = null;
		Object newObj = null;
		
		if(null != params && params.length > 0)
			obj = params[0];
		switch(action){
		case None: 
			break;
		
		case Control:
			if (null != obj){
				if(params.length > 1) timestamp = (Date) params[1];
				if(params.length > 2) comment = (String) params[2];
				if(params.length > 3) vcMode = (VCMode) params[3];
				DB.addToDB(obj, timestamp, comment, vcMode);
			}
			else
				JOptionPane.showMessageDialog(null, "Object is null");
			break;
			
		case Commit:
			if (null != obj){
				if (params.length > 1)
					comment = (String) params[1];
				DB.addVersion(obj,comment);
			}
			else
				JOptionPane.showMessageDialog(null, "Object is null");
			break;
			
		case Rollback:
			if (null == params || params.length == 0){
				Set<Object> db = DB.getAllObjs();
				for (Object obj1 : db) {
					actualObj = obj1;
				}
			}

			else if(params.length==1)
				actualObj = DB.getLatestVersion(obj);
			
			else if(params.length==2){
				if(params[1].getClass().equals(Integer.class)) {
					idx = (Integer) params[1];
					actualObj = DB.getVersion(obj, idx);
				}
				else if(params[1].getClass().equals(Date.class)) {
					timestamp = (Date) params[1];
					actualObj = DB.getVersion(obj, timestamp);
				}
			}
			break;
			
		case Update:
			if (null == obj){
				JOptionPane.showMessageDialog(null, "Object is null");
				break;
			}
			if(params.length == 3){
				newObj = params[2];
				if(params[1].getClass().equals(Integer.class)) {
					idx = (Integer) params[1];
					DB.getVersion(obj, idx).setObject(newObj);
				}
				else if(params[1].getClass().equals(Date.class)) {
					timestamp = (Date) params[1];
					DB.getVersion(obj, timestamp).setObject(newObj);
				}
			}
			else
				JOptionPane.showMessageDialog(null, "Not enough parameters for update");
			break;
			
		case Delete:
			if (null == obj){
				JOptionPane.showMessageDialog(null, "Object is null");
				break;
			}
			if(params.length == 2){
				if(params[1].getClass().equals(DeleteMode.class)){
					delMode = (DeleteMode) params[1];
					if (DeleteMode.DeleteObj.equals(delMode))
						DB.deleteObject(obj);
					else
						DB.deleteObjVersions(obj);
				}
				else if(params[1].getClass().equals(Integer.class)){
					idx = (Integer) params[1];
					DB.deleteVersion(obj, idx);
				}
				else if(params[1].getClass().equals(Date.class)){
					timestamp = (Date) params[1];
					DB.deleteVersion(obj, timestamp);
				}
			}
			else
				JOptionPane.showMessageDialog(null, "Not enough parameters for delete");
			break;
			
		case Comment:
			if (null == obj){
				JOptionPane.showMessageDialog(null, "Object is null");
				break;
			}
			if(params.length == 3){
				comment = (String) params[2];
				if(params[1].getClass().equals(Integer.class)){
					idx = (Integer) params[1];
					DB.getVersion(obj, idx).setComment(comment);
				}
				else if(params[1].getClass().equals(Date.class)){
					timestamp = (Date) params[1];
					DB.getVersion(obj, timestamp).setComment(comment);
				}
			}
			else
				JOptionPane.showMessageDialog(null, "Not enough parameters for comment");
			break;
			
		case Lock:
			if (null != obj)
				DB.lockObject(obj);
			else
				JOptionPane.showMessageDialog(null, "Object is null");
			break;
			
		case Unlock:
			if (null != obj)
				DB.unlockObject(obj);
			else
				JOptionPane.showMessageDialog(null, "Object is null");
			break;
			
		case Compare:
			if (null == obj)
				JOptionPane.showMessageDialog(null, "Object is null");
			else{
				if (params.length==1)
					showCompare(obj, DB.getLatestVersion(obj));
				else if (params.length==2){
					if(params[1].getClass().equals(Integer.class)){
						idx = (Integer) params[1];
						showCompare(obj, DB.getVersion(obj, idx));
					}
					else if(params[1].getClass().equals(Date.class)){
						timestamp = (Date) params[1];
						showCompare(obj, DB.getVersion(obj, timestamp));
					}
				}
			}
			break;
			
		case History:
			if (null != obj){
				ArrayList<Version> list = DB.getVersionList(obj);
				if (null != list)
					showHistory(list);
			}
			else
				JOptionPane.showMessageDialog(null, "Object is null");
			break;
		}
		
	}
	
	private void showHistory(ArrayList<Version> list) {
		// TODO Auto-generated method stub
		
	}

	private void showCompare(Object obj, Version version) {
		// TODO Auto-generated method stub
		
	}*/
}
